#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'

int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	ll a, b, c, d, e, f, g, p, x1, x2, y1, y2;
	cin >> a >> b >> c >> d >> e >> f >> g >> p >> x1 >> x2 >> y1 >> y2;
	ll ans = 0;
	for (int i = x1; i <= x2; i++) {
		for (int j = y1; j <= y2; j++) {
			ll t = (a * i * i * i + b * j * j * j + c * i * i * j + d * i * j * j + e * i * j + f * i + g * j) % p;
			ans = max(ans, t);
		}
	}
	cout << ans << endl;
	return 0;
}